package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.SequenceUtils;
import com.ruoyi.system.domain.TGoodsCarts;
import com.ruoyi.system.domain.TOrdersItems;
import com.ruoyi.system.service.ITGoodsService;
import com.ruoyi.system.service.ITOrdersItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TOrdersMapper;
import com.ruoyi.system.domain.TOrders;
import com.ruoyi.system.service.ITOrdersService;

/**
 * 订单 Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-04-09
 */
@Service
public class TOrdersServiceImpl implements ITOrdersService 
{
    @Autowired
    private TOrdersMapper tOrdersMapper;

    @Autowired
    private ITOrdersItemsService ordersItemsService;

    @Autowired
    private ITGoodsService itGoodsService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询订单 
     * 
     * @param orderId 订单 主键
     * @return 订单 
     */
    @Override
    public TOrders selectTOrdersByOrderId(String orderId)
    {
        return tOrdersMapper.selectTOrdersByOrderId(orderId);
    }

    @Override
    public String getOrderId(){
        String date = SequenceUtils.getCurentDate();
        String key = "serial.gooods:" + date;
        long sequence = redisTemplate.opsForValue().increment(key);
        return date + SequenceUtils.getSequence(sequence);
    }

    /**
     * 查询订单 列表
     * 
     * @param tOrders 订单 
     * @return 订单 
     */
    @Override
    public List<TOrders> selectTOrdersList(TOrders tOrders)
    {
        List<TOrders> list= tOrdersMapper.selectTOrdersList(tOrders);
        TOrdersItems query=new TOrdersItems();
        for(TOrders orders:list){
            query.setOrderId(orders.getOrderId());
            orders.settOrdersItemsList(ordersItemsService.selectTOrdersItemsList(query));
            for(TOrdersItems ordersItems:orders.gettOrdersItemsList()){
                ordersItems.settGoods(itGoodsService.selectTGoodsByGoodsId(ordersItems.getGoodsId()));
            }
        }
        return list;
    }

    /**
     * 新增订单 
     * 
     * @param tOrders 订单 
     * @return 结果
     */
    @Override
    public int insertTOrders(TOrders tOrders)
    {
        return tOrdersMapper.insertTOrders(tOrders);
    }

    /**
     * 修改订单 
     * 
     * @param tOrders 订单 
     * @return 结果
     */
    @Override
    public int updateTOrders(TOrders tOrders)
    {
        return tOrdersMapper.updateTOrders(tOrders);
    }

    /**
     * 批量删除订单 
     * 
     * @param orderIds 需要删除的订单 主键
     * @return 结果
     */
    @Override
    public int deleteTOrdersByOrderIds(String[] orderIds)
    {
        return tOrdersMapper.deleteTOrdersByOrderIds(orderIds);
    }

    /**
     * 删除订单 信息
     * 
     * @param orderId 订单 主键
     * @return 结果
     */
    @Override
    public int deleteTOrdersByOrderId(String orderId)
    {
        return tOrdersMapper.deleteTOrdersByOrderId(orderId);
    }
}
